package com.zhoug.common3.beans;

import com.zhoug.common3.utils.StringUtils;

import java.io.Serializable;

import androidx.annotation.Keep;

/**
 * 日期
 *
 * @Author: zhoug
 * @Date: 2024-01-29
 * @Description:
 */
@Keep
public class ZDate implements Serializable {
    private long timeInMillis;
    private String date;
    private String year;//年
    private String month;//月
    private String day;//日
    private String hour;//时
    private String minute;//分
    private String second;//秒

    private boolean addZero = true;

    public long getTimeInMillis() {
        return timeInMillis;
    }

    public void setTimeInMillis(long timeInMillis) {
        this.timeInMillis = timeInMillis;
    }

    public String getDate() {
        return date;
    }

    public void setDate(String date) {
        this.date = date;
    }

    public String getYear() {
        return year;
    }

    public void setYear(String year) {
        this.year = year;
    }

    public String getMonth() {
        return month;
    }

    public void setMonth(String month) {
        this.month = month;
    }

    public String getDay() {
        return day;
    }

    public void setDay(String day) {
        this.day = day;
    }

    public String getHour() {
        return hour;
    }

    public void setHour(String hour) {
        this.hour = hour;
    }

    public String getMinute() {
        return minute;
    }

    public void setMinute(String minute) {
        this.minute = minute;
    }

    public String getSecond() {
        return second;
    }

    public void setSecond(String second) {
        this.second = second;
    }


    public String getYYYY_MM_DD() {
        StringBuilder builder = new StringBuilder();
        if (!StringUtils.isNullOrEmpty(year)) {
            builder.append(year);
            builder.append("-");
        }
        if (!StringUtils.isNullOrEmpty(month)) {
            builder.append(addZero(month));
            builder.append("-");
        }
        if (!StringUtils.isNullOrEmpty(day)) {
            builder.append(addZero(day));
        }
        String s = builder.toString();
        if (s.length() > 0) {
            s=s.trim();
            if (s.endsWith("-")) {
                s = s.substring(0, s.length() - 1);
            }
        }
        return s;
    }

    public String getHH_MM_SS() {
        StringBuilder builder = new StringBuilder();
        if (!StringUtils.isNullOrEmpty(hour)) {
            builder.append(addZero(hour));
            builder.append(":");
        }
        if (!StringUtils.isNullOrEmpty(minute)) {
            builder.append(addZero(minute));
            builder.append(":");
        }
        if (!StringUtils.isNullOrEmpty(second)) {
            builder.append(addZero(second));
        }
        String s = builder.toString();
        if (s.length() > 0) {
            s=s.trim();
            if (s.endsWith(":")) {
                s = s.substring(0, s.length() - 1);
            }
        }
        return s;
    }

    public String getYYYY_MM_DD_HH_MM_SS() {
        StringBuilder builder = new StringBuilder();
        if (!StringUtils.isNullOrEmpty(year)) {
            builder.append(year);
            builder.append("-");
        }
        if (!StringUtils.isNullOrEmpty(month)) {
            builder.append(addZero(month));
            builder.append("-");
        }
        if (!StringUtils.isNullOrEmpty(day)) {
            builder.append(addZero(day));
            builder.append(" ");
        }
        if (!StringUtils.isNullOrEmpty(hour)) {
            builder.append(addZero(hour));
            builder.append(":");
        }
        if (!StringUtils.isNullOrEmpty(minute)) {
            builder.append(addZero(minute));
            builder.append(":");
        }
        if (!StringUtils.isNullOrEmpty(second)) {
            builder.append(addZero(second));
        }
        String s = builder.toString();
        if (s.length() > 0) {
            s=s.trim();
            if (s.endsWith("-") || s.endsWith("T") || s.endsWith(":")) {
                s = s.substring(0, s.length() - 1);
            }
        }
        return s;
    }

    public void setAddZero(boolean addZero) {
        this.addZero = addZero;
    }

    @Override
    public String toString() {
        return getYYYY_MM_DD_HH_MM_SS();
    }

    public String addZero(int num) {
        return addZero(String.valueOf(num));
    }

    public String addZero(String num) {
        if (addZero && num != null && num.length() == 1) {
            return "0" + num;
        }
        return num;
    }

}
